package demo3;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-02
 * Time: 19:53
 */
public class Test02 {
    String[] arr = {"♣","♦","♥","♠"};
    //生成一副牌
    public List<Card> createCards() {
        List<Card> cards = new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                String cardShape = arr[i];
                int cardSize = j;
                Card card = new Card();
                card.cardSize = cardSize;
                card.cardShape = cardShape;
                cards.add(card);
            }
        }
        return cards;
    }

    //打乱牌(将最后一张牌与前面的牌随机抽取一张交换,从后往前直到第一张牌)
    private void shuffle(List<Card> cards) {
        Random random = new Random();
        for (int i = cards.size() - 1; i > 0; i--) {
            int n = random.nextInt(i);
            swap(cards,i,n);
        }
    }

    //交换牌
    public void swap(List<Card> cards, int a, int b) {
        Card card = cards.get(a);
        cards.set(a,cards.get(b));
        cards.set(b,card);
    }


    public static void main(String[] args) {
        Test02 test02 = new Test02();
        List<Card> cards = test02.createCards();
        System.out.println(cards);
        System.out.println();
        //打乱牌
        test02.shuffle(cards);
        System.out.println(cards);
        System.out.println();
        //三个人轮流抓五张牌
        List<List<Card>> list = new ArrayList<>(3);
        list.add(new ArrayList<>());
        list.add(new ArrayList<>());
        list.add(new ArrayList<>());
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                list.get(j).add(cards.remove(0));
            }
        }
        System.out.println("A:");
        System.out.println(list.get(0));
        System.out.println("B:");
        System.out.println(list.get(1));
        System.out.println("C:");
        System.out.println(list.get(2));

    }
}
